﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyFavorites.Model;
using System.Security.Cryptography;
using System.Text;

/// <summary>
///UserHelper 的摘要说明
/// </summary>
public class UserHelper
{
    C_User _user = new C_User();
    Entities entites = new Entities();
    public UserHelper(C_User user)
    {
        _user = user;
    }
    /// <summary>
    /// 判断用户是否存在
    /// </summary>
    /// <returns></returns>
    public bool IsExists()
    {
        bool IsExists = false;
        C_User IsExistsUser = entites.C_User.SingleOrDefault(u => u.UserName == _user.UserName);
        if (IsExistsUser != null)
        {
            IsExists = true;
        }
        return IsExists;
    }
    /// <summary>
    /// 增加一个新用户
    /// </summary>
    /// <returns></returns>
    public C_User AddUser()
    {
        if (IsExists() == false)
        {
            _user.Password = Common.GetMd5Hash(_user.Password);
            entites.C_User.AddObject(_user);
        }
        else
        {
            throw new Exception("");
        }
        return _user;
    }
}